package code.sbxj.qwd.com.sbxj;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.CacheDiskUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.UnsupportedEncodingException;


@ContentView(R.layout.activity_devicelist)
public class DeviceListActivity extends BaseActivity {

    @ViewInject(R.id.listview)
    private ListView listview;

    @ViewInject(R.id.errorlayout)
    private LinearLayout errorlayout;

    @ViewInject(R.id.ed_error)
    private EditText ed_error;

    @ViewInject(R.id.save)
    private Button save;

    @ViewInject(R.id.titleright)
    private TextView titleright;

    MyAdapter myadapter;
    JSONArray dataArray = new JSONArray();

    int edindex = -1;

    int currentSelsectedIndex = 0;
    boolean isoffline;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isoffline = (boolean) CacheDiskUtils.getInstance().getSerializable("isoffline", false);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setTitleText("巡检线：" + getIntent().getExtras().getString("linename") + " - " + getIntent().getExtras().getString("devicename"));
        initView();
        getTasklist(getIntent().getExtras().getInt("deviceid") + "");
    }

//    @Override
//    protected void onResume() {
//        super.onResume();
//        getTasklist(getIntent().getExtras().getInt("deviceid") + "");
//    }

    void initView() {
        myadapter = new MyAdapter();
        listview.setAdapter(myadapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Intent intent = new Intent();
                intent.setClass(DeviceListActivity.this, CheckItermActivity
                        .class);
                intent.putExtra("linename", getIntent().getExtras().getString("linename"));
                intent.putExtra("lineid", getIntent().getExtras().getInt("lineid"));

                try {
                    intent.putExtra("deviceId", dataArray.getJSONObject(i).getInt("id"));
                    intent.putExtra("devicename", dataArray.getJSONObject(i).optString("name"));
                    intent.putExtra("data", dataArray.getJSONObject(i).toString());
                    CacheDiskUtils.getInstance().put("checkiterm", dataArray.getJSONObject(i));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                startActivity(intent);


//                Intent intent = new Intent();
//                intent.setClass(DeviceListActivity.this, DeviceDetailActivity
//                        .class);
//                intent.putExtra("taskId", getIntent().getExtras().getInt("lineid"));
//                intent.putExtra("deviceId", getIntent().getExtras().getInt("deviceid"));
//                try {
//                    intent.putExtra("data", dataArray.getJSONObject(i).toString());
//                    CacheDiskUtils.getInstance().put("devicedetail", dataArray.getJSONObject(i));
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//
//                startActivity(intent);
            }
        });
        titleright.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return dataArray.length();
        }

        @Override
        public Object getItem(int position) {
            try {
                return dataArray.get(position);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            final int index = position;
            convertView = getLayoutInflater().inflate(R.layout.activity_deviceiterm, null);

            TextView linename = convertView.findViewById(R.id.linename);
            EditText ed = convertView.findViewById(R.id.ed);
            TextView photo = convertView.findViewById(R.id.photo);
            photo.setText("检查项");

            try {
                linename.setText("设备：" + dataArray.getJSONObject(position).optString("name"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return convertView;
        }


    }




    private void getTasklist(String deviceid) {

        if (isoffline) {
            try {
                dataArray = CacheDiskUtils.getInstance().getJSONObject("devicelist").getJSONArray("deviceList");
                myadapter.notifyDataSetChanged();
                return;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        ShowLoading();
        RequestParams params = new RequestParams(Config.BaseUrl + "task/uncheckItems?deviceId=" + deviceid + "&taskId=" + getIntent().getExtras().getInt("lineid"));
        params.setUseCookie(true);
        params.addHeader("cookie", CacheDiskUtils.getInstance().getString("cookie"));
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                Log.e("result", result);

                try {
                    dataArray = new JSONArray(result);
                    myadapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {
                CancleLoading();
            }
        });
    }
}
